<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generateds a Cairngorm command for a given table
 */
function generateEventForTable( $table ) {
	$assoc = $table[0][2];
	//var_dump( $table );
	$className = toCamelCase( $assoc['table_name'] );
	$classNameUp = strtoupper( $className );
	$classNameLow = strtolower( $className );
	$tableName = $assoc['table_name'];
	$columns = getTableColumns( $tableName );
	$columnsString = join(', ',array_keys($columns));
	$primaryKey =
		getPKey( $table );
		//toCamelCaseMember($assoc['column_name']);
	$secondaryKeys = array();
	foreach( $columns as $key=>$vale ) {
		$secondaryKeys[$key] = $columns[$key];
	}
	//$secondaryKeys = StdFunc::array_clone( $columns );
	unset( $secondaryKeys[$primaryKey] );

	# create the require_once path
	$requireOnceVoPath = "";

	$export =
'package '.con::$package.'.event
{
	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

	import com.adobe.cairngorm.control.CairngormEvent;
	import '.con::$package.'.vo.'.$className.';
	import com.crazedcoders.lib.interfaces.IView;

	public class '.$className.'Event extends CairngormEvent
	{
		public static const '.$classNameUp.'_ACTION_COUNT_ALL:String =
			"'.$classNameLow.'ActionCountAll-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_GET_ALL:String =
			"'.$classNameLow.'ActionGetAll-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_GET_ALL_BY_START_LENGTH:String =
			"'.$classNameLow.'ActionGetAllByStartLength-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_DELETE_ONE:String =
			"'.$classNameLow.'ActionDeleteOne-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_CREATE_ONE:String =
			"'.$classNameLow.'ActionCreateOne-'.$className.'Event";
';
	if( ! array_key_exists('isManyToMany', $table )
			|| ! $table['isManyToMany'] ) {
		$export .=
'		public static const '.$classNameUp.'_ACTION_UPDATE_ONE:String =
			"'.$classNameLow.'ActionUpdateOne-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_GET_ONE:String =
			"'.$classNameLow.'ActionGetOne-'.$className.'Event";
';
	}
	else {
		$export .=
'		public static const '.$classNameUp.'_ACTION_GET_ALL_BY_'.
			strtoupper($table[0][2]['column_name']).'_'.
			strtoupper($table[1][2]['column_name']).':String =
			"'.$classNameLow.'ActionGetAllBy'.
			($table[0][2]['column_name']).''.
			($table[1][2]['column_name']).'-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_GET_ALL_BY_'.
			strtoupper($table[0][2]['column_name']).':String =
			"'.$classNameLow.'ActionGetAllBy'.
			($table[0][2]['column_name']).'-'.$className.'Event";
		public static const '.$classNameUp.'_ACTION_GET_ALL_BY_'.
			strtoupper($table[1][2]['column_name']).':String =
			"'.$classNameLow.'ActionGetAllBy'.
			($table[1][2]['column_name']).'-'.$className.'Event";
';
	}
	$export .=
'		//include "'.toCamelCase($className).'_include.as";

		/*
		 * parameters passed to php where applicable
		 */
		public var paramItem:'.$className.';
		public var paramStart:int;
		public var paramLength:int;
		public var paramPrimaryKey:int;
';

	if( array_key_exists('isManyToMany', $table )
			&& $table['isManyToMany'] ) {
		$export .=
'
		public var paramMapping:Array;
';
	}
	$export .=
'

		/**
		 * useCache determines whether the command should use chached data
		 * if available, or get data from the server always.
		 *
		 * defaults to using cached data (true)
		 */
		public var useCache:Boolean = true;

		public function '.$className.'Event(type:String, view:IView = null,
				bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
			//set data after super()
			this.data = view;
		}
	}
}

';

	return $export;

}

?>
